package com.vhall.component.plugin.common.config;

import com.vhall.component.plugin.common.filter.EncryptionFilter;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * 解密处理，自动配置, 当为web时候自动生效
 *
 * @author yuanzh
 */
@Configuration
@AutoConfigureAfter(WebAutoConfigure.class)
public class EncryptionAutoConfig {

    /**
     * 创建 EncryptionFilter Bean，用于解密处理
     */
    @Bean
    public FilterRegistrationBean<EncryptionFilter> encryptionFilter() {
        return createFilterBean(new EncryptionFilter());
    }

    private static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter) {
        FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
        bean.setOrder(com.vhall.component.framework.common.constant.FilterOrder.DECRYPTION_ORDER);
        return bean;
    }

}
